$(function () {
    var index1 = false;
    var index2 = false;
    var index3 = false;
    /*验证码，验证*/
    var verifyCode = new GVerify("v_container");
    $("#user_verification").blur(function () {
        yanzhengma();
    })

    function yanzhengma() {
        var code = $("#user_verification").val();
        if (verifyCode.validate(code)) {
            $("#user_verification_span").text("验证正确").css("color", "green");
            index1 = true;
        } else {
            $("#user_verification_span").text("验证码错误").css("color", "red");
        }
    }

    /*验证账户不能为空*/
    $("#user_name").blur(function () {
        var name = $("#user_name").val()
        if (name == null) {
            $("#user_name_span").text("账号不能为空").css("color", "red");
        } else {
            index2 = true;
        }
    })
    /*验证密码不能为空*/
    $("#user_password").blur(function () {
        var password = $("#user_password").val()
        if (password == null) {
            $("#user_password_span").text("密码不能为空").css("color", "red");
        } else {
            index3 = true;
        }
    })


    /*登录方法实现*/
    $("#user_verification").keydown(function () {
        if (event.keyCode == 13) {
            yanzhengma();
            loginStr();
        }
    })
    document.onkeydown = function () {
        if (event.keyCode == 13) {
            loginStr();
        }
    }
    $("#btn_from").click(function () {
        loginStr();
    })

    /**
     * 登录方法
     */
    function loginStr() {
        if (index2 == true && index3 == true) {
            if (index1 == true) {
                var name = $("#user_name").val();
                var password = $("#user_password").val();
                $.ajax({
                    url: "../../loginRegistered/findByUsernamePassword",
                    data: {
                        name: name,
                        password: password
                    },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        if (data.count == 1) {
                            /*获取登陆者信息*/
                            var meaningUser = data.meaningUser;
                            console.log("meaningUser.userState--" + meaningUser.userState)
                            console.log("meaningUser.username--" + meaningUser.username)
                            var username = meaningUser.username;
                            var userState = meaningUser.userState;
                            $.cookie("username", meaningUser.username, {expire: 1});
                            $.cookie("userState", meaningUser.userState, {expire: 1});
                            if (username == "admin") {
                                window.location.href = '../../index.jsp?username=' + username + '&userState=' + userState + '';
                            } else {
                                if (userState == 2) {
                                    window.location.href = '../../index.jsp?username=' + username + '&userState=' + userState + '';
                                } else {
                                    window.location.href = "../ClinicalDataQuerySystem/choose.jsp";
                                }
                            }
                            index1 = false;
                        } else if (data.count == 2) {
                            $("#user_login").text("用户不存在").css("color", "red");
                            $("#user_verification").val("");
                            $("#user_password").val("");
                        } else if (data.count == 3) {
                            $("#user_login").text("该用户正在审核中。。。").css("color", "red");
                            $("#user_verification").val("");
                            $("#user_password").val("");
                        } else if (data.count == 4) {
                            $("#user_login").text("该用户审核未通过").css("color", "red");
                            $("#user_verification").val("");
                            $("#user_password").val("");
                        } else if (data.count == 5) {
                            $("#user_login").text("用户所对应的密码错误").css("color", "red");
                            $("#user_verification").val("");
                            $("#user_password").val("");
                        }
                    },
                    error: function () {
                        window.location.href = "../../404.jsp";
                    }
                });
            } else {
                $("#user_v_code").text("验证码错误").css("color", "red");
            }
        } else {
            $("#user_v_code").text("账号或者密码不能为空").css("color", "red");
        }
    }

    /*轮播图，设置图片轮播时间*/
    $('#myCarousel').carousel({
        interval: 1000
    })

})